package com.um.springboot.starter.orm.params;

import com.um.springboot.starter.dao.ColumnUtil;
import com.um.springboot.starter.utils.ReflectUtil;
import com.um.springboot.starter.utils.StringUtil;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 邓南辉
 * 日期 2023/6/20
 * 版权所有 广州优匠科技有限公司
 */
@Data
public abstract class ListDTO implements Serializable {

   // private String orderby;

    public abstract ListParams toListParams();

    public ListParams toListParams(String[] sProperties,String[] tProperties){
        ListParams listParams = new ListParams();

        for(int index = 0;index < sProperties.length;index ++){
            Object value = ReflectUtil.getValue(this,sProperties[index]);
            if(!StringUtil.getTrim(value).equals("")) {
                listParams.getCondition().put(tProperties[index], StringUtil.getTrim(value));
            }
        }

//        if(!StringUtil.getTrim(orderby).equals("") && ColumnUtil.isSqlPropertyNameValid(orderby)){
//            listParams.getCondition().put("orderby",orderby);
//        }

        return listParams;
    }

    public ListParams toListParams(String[] sProperties,String[] tProperties,String rangeName,String beginName,String endName){
        ListParams listParams = new ListParams();

        for(int index = 0;index < sProperties.length;index ++){
            Object value = ReflectUtil.getValue(this,sProperties[index]);
            if(!StringUtil.getTrim(value).equals("")) {
                listParams.getCondition().put(tProperties[index], StringUtil.getTrim(value));
            }
        }

//        if(!StringUtil.getTrim(orderby).equals("") && ColumnUtil.isSqlPropertyNameValid(orderby)){
//            listParams.getCondition().put("orderby",orderby);
//        }

        List<RangeParams> rangeParamsList = new ArrayList<>();


        Object beginValue = ReflectUtil.getValue(this,beginName);
        if(!StringUtil.getTrim(beginValue).equals("")){
            RangeParams rangeParams = new RangeParams();
            rangeParams.setValue(StringUtil.getTrim(beginValue));
            rangeParams.setRange("begin");
            rangeParamsList.add(rangeParams);
        }

        Object endValue = ReflectUtil.getValue(this,endName);
        if(!StringUtil.getTrim(endValue).equals("")){
            RangeParams rangeParams = new RangeParams();
            rangeParams.setValue(StringUtil.getTrim(endValue));
            rangeParams.setRange("end");
            rangeParamsList.add(rangeParams);
        }

        if(rangeParamsList.size() > 0)
            listParams.getCustom().getRangeField().put(rangeName,rangeParamsList);

        return listParams;
    }
}
